import 'dart:convert';

import 'package:bxultimate/data/request/application_request.dart';
import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/shared/constant/app_constant.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'state.dart';

class QuestionnaireDetailController extends GetxController {
  final QuestionnaireDetailState state = QuestionnaireDetailState();

  final id = Get.arguments['id'];

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();
    state.dto = await ApplicationRequest.getQuestionnaireDetail(id);
    update();
  }

  void submit() async{
    print(state.dto.data![0].userAnswer);
    var data = Map();

    state.dto.data!.forEach((element) {
      data[element.id] = element.userAnswer;
    });

    Result result = await ApplicationRequest.submitWjdc(json.encode(data));

    if(result.code == AppConstant.RESULT_ERROR){
      SmartDialog.showNotify(msg: '提交失败', notifyType: NotifyType.error);
      return;
    }

    SmartDialog.showNotify(msg: '提交成功', notifyType: NotifyType.success);
    Get.back();

  }
}
